package com.ccys.cms.controller.corporateAndInsurance;


import com.ccys.cms.service.corporateAndInsurance.CDMPBenefitPlanService;
import com.ccys.cms.service.corporateAndInsurance.CDMPBenefitService;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitSService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.corporateAndInsurance.CDMPBenefitDto;
import com.ccys.db.dto.corporateAndInsurance.CDMPBenefitPageDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.entity.TblCdmpBenefit;
import com.ccys.db.entity.TblCdmpBenefitPlan;
import com.ccys.db.entity.TblPlanBenefitService;
import com.ccys.db.vo.corporateAndInsurance.CDMPBenefitVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/corporateAndInsurance")
@Api(tags = "cdmpBenefit")
public class CDMPBenefitController {


    @Resource
    private CDMPBenefitService service;

    @Resource
    private PlanBenefitSService benefitSService;

    @Resource
    private CDMPBenefitPlanService planService;




    @PostMapping("cdmpBenefit")
    @ApiOperation("新增")
    @Transactional
    public ResponseResult insert(@RequestBody CDMPBenefitDto dto) {

        return service.change(dto);
    }

    @PutMapping("cdmpBenefit")
    @ApiOperation("修改")
    @Transactional
    public ResponseResult update(@RequestBody CDMPBenefitDto dto) {

        return service.change(dto);
    }


    @GetMapping("cdmpBenefit/detail")
    @ApiOperation("根据id查询")
    public ResponseResult<CDMPBenefitDto, Object> getById(String id) {
        TblCdmpBenefit entityById = service.findEntityById(id);
        TblPlanBenefitService oneByLimit = benefitSService.findOneByLimit(TblPlanBenefitService.class, new ArrayList<String>() {{
            add("del_flag = 0");
            add(" benefit_id = '" + id + "'");

        }});
        CDMPBenefitDto dto = new CDMPBenefitDto();
        dto.setCdmpBenefit(entityById);
        dto.setBenefitService(oneByLimit);
        List<TblCdmpBenefitPlan> policyList = planService.findListByLimit(TblCdmpBenefitPlan.class, new ArrayList<String>() {{
            add("del_flag = 0");
            add(" benefit_id = '" + id + "'");
        }}, "created_on desc");
        dto.setPolicyList(policyList);
        return ResponseResult.success(dto);
    }


    @DeleteMapping("cdmpBenefit")
    @ApiOperation("根据Id删除")
    @Transactional
    public ResponseResult delete(String id) {

        TblCdmpBenefit model = new TblCdmpBenefit();
        model.setId(id);
        model.setDelFlag(NumEnum.DEL.getCode());
        int i = service.updateEntityById(model);
        //删除benefitService
        benefitSService.deleteBenefitService(id);
        //删除plan
        planService.deletePlan(id);
        return ResponseResult.update(i);
    }


    @GetMapping("cdmpBenefit")
    @ApiOperation(value = "查询分页", notes = "排序字段  t1.status /t2. user_fullname / t1.created_on ")
    public ResponseResult<PageInfo<CDMPBenefitVo>, Object> getPage(CDMPBenefitPageDto dto) {
        return service.getPage(dto);
    }
}
